package com.koron.css2.meterService.impl;

import com.koron.css2.meterService.bean.MsInfoBean;
import com.koron.css2.meterService.bean.MsInfoPlanDetailBean;
import com.koron.css2.meterService.dto.MsInfoPlanDto;
import com.koron.css2.meterService.mapper.MsInfoMapper;
import com.koron.css2.meterService.mapper.MsInfoPlanDetailMapper;
import com.koron.css2.meterService.mapper.MsInfoPlanMapper;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.workflow.ProcessInterface;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.beans.BeanUtils;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 水表入库流程回调处理
 * @author 周利斌
 *
 */
public class MsStorageWorkflow implements ProcessInterface{
	
	private static Logger log = LogManager.getLogger(MsStorageWorkflow.class);

	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, String billId, String fromNodeCode, int operation) {
		if("Process-task1".equals(fromNodeCode) && OPERATION_AGREE == operation) {
			MsInfoPlanMapper mapper = factory.getMapper(MsInfoPlanMapper.class);
			MsInfoPlanDetailMapper detailMapper = factory.getMapper(MsInfoPlanDetailMapper.class);
			MsInfoMapper msInfoMapper = factory.getMapper(MsInfoMapper.class);
			MsInfoPlanDto msInfoPlan = mapper.selectMsInfoPlan(billId);
			List<MsInfoPlanDetailBean> msInfoPlanDetails = detailMapper.selectByReceiptId(billId);
			List<MsInfoBean> list=new ArrayList<MsInfoBean>();
			for(MsInfoPlanDetailBean msInfoPlanDetail : msInfoPlanDetails) {
				MsInfoBean msInfo = new MsInfoBean();
				BeanUtils.copyProperties(msInfoPlan, msInfo);
				msInfo.setMeterNo(msInfoPlanDetail.getMeterNo());
				msInfo.setFactoryNo(msInfoPlanDetail.getFactoryNo());
				msInfo.setMeterCert(msInfoPlanDetail.getMeterCert());
				msInfo.setCreateName(userInfo.getUserInfo().getName());
				msInfo.setId(Tools.getObjectId());
				msInfo.setMeterStatus(1);
				int iCount = msInfoMapper.addMsInfo(msInfo);
				if(iCount > 0) {
					log.info("水表添加成功!水表编号：{}", msInfo.getMeterNo());
					list.add(msInfo);
				}
			}
			//同步水表信息至集抄系统
			if(list!=null&&list.size()>0) {
				
			}
		}
		return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "执行成功", Void.class);
	}

	@Override
	public MessageBean<?> before(SessionFactory factory, UserInfoBean userInfo, String billId, String fromNodeCode, int operation) {
		return exec(factory, userInfo, billId, fromNodeCode, operation);
	}

	@Override
	public MessageBean<?> after(SessionFactory factory, UserInfoBean userInfo, String billId, String fromNodeCode, String toNodeCode) {
		return null;
	}

}
